<!-- 付款记录推送 -->
<template>
    <div class="pushPaymentRecord">
    	<el-breadcrumb separator="/" class="bread-title">
    	     <el-breadcrumb-item >配置中心</el-breadcrumb-item>
    	     <el-breadcrumb-item>接口配置</el-breadcrumb-item>
    	     <el-breadcrumb-item>付款记录推送</el-breadcrumb-item>
    	</el-breadcrumb>
		<el-form :inline="true" :model="ruleForm" class="demo-form-inline">
			<el-form-item label="城市：">
			    <el-select v-model="ruleForm.city" filterable size="small" @change="citychange">
			        <el-option v-for="(item,index) in citys"
			                   :key="index"
			                   :label="item.city"
			                   :value="item.cityId">
			        </el-option>
			    </el-select>
			</el-form-item>
			<el-form-item label="楼盘：">
			    <el-select v-model="ruleForm.devName" filterable size="small" @change="devchange">
			        <el-option v-for="(item,index) in estateslist"
				        :key="index"
				        :label="item.devName"
				        :value="item.devId">
			        </el-option>
			    </el-select>
			</el-form-item>
			<el-form-item label="楼栋：">
			    <el-select v-model="ruleForm.floor" filterable size="small" @change="buildchange">
			        <el-option v-for="(item,index) in housebuilding"
			                   :key="index"
			                   :label="item.buildName"
			                   :value="item.buildName">
			        </el-option>
			    </el-select>
			</el-form-item>
			<el-form-item label="单元：">
			    <el-select v-model="ruleForm.unit" filterable size="small" @change="unitchange">
			        <el-option v-for="(item,index) in unitlist"
			                   :key="index"
			                   :label="item.id"
			                   :value="item.id">
			        </el-option>
			    </el-select>
			</el-form-item>
			<el-form-item label="楼层：">
			    <el-select v-model="ruleForm.buildId" filterable size="small" @change="floorchange">
			        <el-option v-for="(item,index) in buildlist"
			                   :key="index"
			                   :label="item.id"
			                   :value="item.id">
			        </el-option>
			    </el-select>
			</el-form-item>
			<el-form-item label="房号：">
			    <el-select v-model="ruleForm.room" filterable size="small">
			        <el-option v-for="(item,index) in roomNumlist"
			                   :key="index"
			                   :label="item.id"
			                   :value="item.id">
			        </el-option>
			    </el-select>
			</el-form-item>
			<el-form-item label="客户：">
				<el-input v-model.trim="ruleForm.customer" placeholder="请输入客户姓名/手机号" size="small"></el-input>
			</el-form-item>
			<el-form-item label="推送状态：">
				<el-select v-model="ruleForm.pushStatus" filterable size="small">
				    <el-option label="全部" :value="undefined"></el-option>
				    <el-option label="成功" :value="1"></el-option>
				    <el-option label="失败" :value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="佣金支付方式：">
			    <el-select v-model="ruleForm.payWay" filterable size="small">
			        <el-option v-for="(item,index) in commissionPayment"
			                   :key="index"
			                   :label="item.label"
			                   :value="item.name">
			        </el-option>
			    </el-select>
			</el-form-item>
			<el-form-item label="推送时间" prop="time">
			  <el-date-picker
			  	size="small"
			    v-model="time"
			    type="datetimerange"
			    range-separator="至"
			    start-placeholder="开始日期"
			    end-placeholder="结束日期"
			    :picker-options="pickerOptions"
			    @change="changeDate"
			  >
			  </el-date-picker>
			</el-form-item>
			<el-form-item label="推荐人：">
				<el-input v-model.trim="ruleForm.broker" placeholder="请输入客户姓名/手机号" size="small"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="btnhander('search')" size="small" id="monitorEnter">查询</el-button>
				<el-button  @click="btnhander('reset')" size="small">重置</el-button>
				<el-button type="primary" @click="btnhander('push')" size="small">推送</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="tableData" border  style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" :selectable='checkboxT'></el-table-column>
			<el-table-column type="index" label="序号" width="50" align="center" fixed>
				<template slot-scope='scope'>
					<span v-if="scope.$index==0">合计</span>
					<span v-else>{{scope.$index}}</span>
				</template>
			</el-table-column>
			<el-table-column align="center" prop="brokerName" label="推荐人" width="150">
				<template slot-scope='scope'>
					<span>{{scope.row.brokerName}}{{scope.row.brokerPhone}}</span>
				</template>
			</el-table-column>
			<el-table-column align="center" prop="customerName" label="客户姓名"></el-table-column>
			<el-table-column align="center" prop="customerPhone" label="客户手机号" width="110" :formatter="initphone"></el-table-column>
			<el-table-column align="center" prop="city" label="城市"></el-table-column>
			<el-table-column align="center" prop="devName" label="楼盘"  width="140"></el-table-column>
			<el-table-column align="center" prop="roomNumber" label="房间号"  width="140"></el-table-column>
			<el-table-column align="center" prop="dealPrice" label="成交价(元)" width="140" :formatter="initCase"></el-table-column>
			<el-table-column align="center" prop="commissionRule" label="佣金标准"></el-table-column>
			<el-table-column align="center" prop="amountPay" label="推荐人应结佣金额(元)" width="140" :formatter="initCase"></el-table-column>
			<el-table-column align="center" prop="alreadyPay" label="推荐人已结佣金(元)" width="150" :formatter="initCase"></el-table-column>	
			<el-table-column align="center" prop="applyTimeFirst" label="申请结佣时间1" width="160" :formatter="initTimer"></el-table-column>
			<el-table-column align="center" prop="alreadyTimeFirst" label="付款完成时间1" width="160" :formatter="initTimer"></el-table-column>
			<el-table-column align="center" prop="applyTimeSecond" label="申请结佣时间2" width="160" :formatter="initTimer"></el-table-column>
			<el-table-column align="center" prop="alreadyTimeSecond" label="付款完成时间2" width="160" :formatter="initTimer"></el-table-column>
			<el-table-column align="center" prop="payWay" label="佣金支付方式" width="160" :formatter="initpayWay"></el-table-column>
			<el-table-column align="center" prop="pushTime" label="最新推送时间" width="160" :formatter="initTimer"></el-table-column>
			<el-table-column align="center" prop="pushStatus" label="最新推送状态" width="110" :formatter="initpushStatus"></el-table-column>
			<el-table-column align="center" label="记录" width="90" fixed='right'>
				<template slot-scope='scope'>
					<span v-if="scope.$index==0">———</span>
					<el-button v-else type="text" @click="$router.push({path:'/pushRecordDetail',query:{crmCid:scope.row.crmCid,crmRoomGuid:scope.row.crmRoomGuid,payWay:scope.row.payWay}})">查看</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="pagebar">
		  <el-pagination
		    background
		    layout="total, sizes, prev, pager, next"
		    :current-page.sync="ruleForm.pageNum"
		    :page-sizes="[10, 20, 30, 40]"
		    :page-size="ruleForm.pageSize"
		    @size-change="handleSizeChange"
		    @current-change="handleCurrentChange"
		    :total="totalSize">
		  </el-pagination>
		</div>
    </div>
</template>

<script>
import api from "@/api/apiPath"
import utils from "@/common/utils.js"

export default {
    name:"pushPaymentRecord",
    title:'付款记录推送',
    data() {
        return {
        	issend:true,
        	citys:[],
        	estateslist:[],
        	housebuilding:[],//楼栋
        	unitlist:[],//单元
        	roomNumlist:[],
        	room:[],//房号
        	buildlist:[],
        	commissionPayment:[
        		{label:'全部',name:''},
        		{label:'好生活资金池支付',name:1},
        		{label:'好生活垫付',name:2}
        		],//佣金支付方式
        	time:[],
        	multipleSelection:[],
        	tableData:[],
        	ruleForm:{
        		city:undefined,//城市
        		pushStatus:undefined,//推送状态：1-成功，2-失败
        		devName:'',//楼盘名称
        		floor:'',//楼栋
        		unit:'',//单元
        		buildId:'',//楼层
        		room:'',//房号
        		customer:'',//客户
        		payWay:'',//支付方式
        		bigenTime:'',//开始时间
        		endTime:'', //结束时间
                broker:'',//推荐人
                pageSize:10,
                pageNum:0,
        	},
        	totalSize:0,
        	pickerOptions: {
        	  disabledDate(time) {
        	     return time.getTime() > Date.now() - 8.64e6;
        	  }
        	}
        }
    },
    created() {
    	//第一次进入或者缓存后刷新浏览器页面缓存丢失都会请求
		this.loadCity()
		this.allestate()
		this.getlistdata()
		this.issend = false
    },
    activated(){
		//详情页决定是否发送数据
		this.issend = this.$route.query.issend
    	if (this.issend) {
			this.getlistdata()
			this.issend = false
    	}
    },
    mounted() {
    },
    methods: {
		async getlistdata(){
			try {
				let response1 = await this.getlist()
				let response2 = await this.getAmountTotal()
			} catch(err){
				console.error(err)
			}
		},
		getlist(){
			return new Promise((resolve,rej) => {
				this.$axios.post('/forcrm/commission/pay/getCommissionLogList',this.ruleForm).then((res)=>{
					this.tableData = res.list
					this.totalSize = res.total
					resolve(res.list)
				})
			})
		},
    	//合计数据
		getAmountTotal(data){
			return new Promise((resolve,rej) => {
				this.$axios.post('/forcrm/commission/pay/selectAmountTotle',{}).then(res=>{
					let obj = {
						brokerName: "———",
						customerName: "———",
						customerPhone: "",
						city: "———",
						devName: "———",
						roomNumber: "———",
						dealPrice: "",
						commissionRule: "———",
						amountPay: res.amountPayTotle,
						alreadyPay: res.alreadyPayTotle,
						applyTimeFirst: "",
						alreadyTimeFirst: "",
						applyTimeSecond: "",
						alreadyTimeSecond: "",
						payWay: "",
						pushTime: "",
						pushStatus: "",
						createTime: "———",
						nodeName: "———",
						followType: "———"
					}
					this.tableData.unshift(obj)
				})
			})
		},
    	//楼栋
    	devchange(){
    		this.ruleForm.floor = ''
    		this.ruleForm.unit = ''
    		this.ruleForm.buildId = ''
    		this.ruleForm.room = ''
    		this.unitlist = []
    		this.buildlist = []
    		this.roomNumlist = []
    		this.$axios.post('/estate/estateHouse/selectBuildByDevId',{devId:this.ruleForm.devName}).then(res=>{
				this.housebuilding = res
    		})
    	},
    	//单元
    	buildchange(){
    		this.ruleForm.unit = ''
			this.ruleForm.buildId = ''
			this.ruleForm.room = ''
			this.buildlist = []
			this.roomNumlist = []
			this.$axios.post('/estate/estateHouse/selectHouseUnit',{devId:this.ruleForm.devName,buildId:this.ruleForm.floor}).then(res=>{
				this.unitlist = res
			})
    	},
		//楼层
		unitchange(){
			this.ruleForm.buildId = ''
			this.ruleForm.room = ''
			this.roomNumlist = []
			this.$axios.post('/estate/estateHouse/selectHouseFloor',{devId:this.ruleForm.devName,buildId:this.ruleForm.floor,unit:this.ruleForm.unit}).then((res)=>{
				this.buildlist = res
			})
		},
    	//房号
    	floorchange(){
    		this.ruleForm.room = ''
			this.$axios.post('/estate/estateHouse/selectHouseCellName',{devId:this.ruleForm.devName,buildId:this.ruleForm.floor,unit:this.ruleForm.unit,floor:this.ruleForm.buildId}).then(res=>{
				this.roomNumlist = res
			})
    	},
    	citychange(){
    		this.ruleForm.floor = ''
    		this.ruleForm.unit = ''
    		this.ruleForm.buildId = ''
    		this.ruleForm.room = ''
    		this.ruleForm.devName = ''
			this.ruleForm.housebuilding = []
    		this.unitlist = []
    		this.buildlist = []
    		this.roomNumlist = []
			this.allestate()
    	},
		allestate(){
			this.axios.post(api.ALLESTATEDEVS,{cityId:this.ruleForm.city}).then(res=>{
				if (res.data.code == 200) {
					this.estateslist = res.data.data
					this.estateslist.unshift({devId:'',devName:'全部'})
				}
			})
		},
    	initCase(row, column, cellValue, index){
    		if (cellValue === '') {
    			return '———'
    		}else {
    			return cellValue? utils.formatMoney(cellValue): "0.00"            
    		}
    	},
    	initTimer(row, column, cellValue, index){
    		if (cellValue === '') {
    			return '———'
    		}else {
    			return utils.getTimeString(cellValue)
    		}			
    	},
		initphone(row, column, cellValue, index){
			if (cellValue === '') {
				return '———'
			}else {
				return utils.formatPhoneNumber(cellValue)
			}
		},
    	initpayWay(row, column, cellValue, index){
    		if (cellValue === '') {
    			return '———'
    		}
    		if (cellValue == 1) {
    			return '好生活资金池支付'
    		}else{
    			return '好生活垫付'
    		}
    	},
    	initpushStatus(row, column, cellValue, index){
    		if (cellValue === '') {
    			return '———'
    		}
			if (cellValue == 1) {
				return '成功'
			}else{
				return '失败'
			}
    	},
    	handleCurrentChange(){
    		this.getlistdata()
    	},
    	handleSizeChange(val){
    		this.ruleForm.pageSize = val
    		this.getlistdata()
    	},
    	checkboxT(row,index){
    		//第一条数据合计禁掉
    		if (row.pushStatus == 1 || index == 0) {
    			return false
    		}else{
    			return true
    		}
    	},
    	handleSelectionChange(val) {
    		this.multipleSelection = val;
    	},
    	btnhander(type){
    		switch(type){
    			case 'search':
    				this.ruleForm.pageNum =1
    				this.getlistdata()
    				break
    			case 'reset':
    			 	this.ruleForm.city = undefined
    			 	this.ruleForm.pushStatus = undefined
    			 	this.ruleForm.devName = ''
    			 	this.ruleForm.floor = ''
    			 	this.ruleForm.unit = ''
    			 	this.ruleForm.buildId = ''
    			 	this.ruleForm.room = ''
    			 	this.ruleForm.customer = ''
    			 	this.ruleForm.payWay = 	''
    			 	this.ruleForm.bigenTime = ''
    			 	this.ruleForm.endTime = ''
    			 	this.ruleForm.broker = ''
    			 	this.time = ['','']
    			 	break
    			case 'push':
					if (this.multipleSelection.length>0) {
						let arrtemp = [];
						this.multipleSelection.forEach(item => {
							let obj = {}
							obj.crmCid = item.crmCid
							obj.crmRoomGuid = item.crmRoomGuid
							arrtemp.push(obj)
						})
						this.$confirm('确认推送?', '提示', {
					          confirmButtonText: '确定',
					          cancelButtonText: '取消',
					          type: 'warning'
					        }).then(() => {
					        	this.$axios.post('/forcrm/commission/pay/pushToCrm',arrtemp).then(res=>{
					        		this.$message({
					        			type: "success",
					        			message: `推送成功${res.successNum}条,失败${res.failNum}条`
					        		})
					        		this.getlistdata()
					        	})
					        }).catch(() => {
					          this.$message({
					            type: 'info',
					            message: '已取消'
					          });          
					        });
					  
					}else {
						this.$message({
							type: "warning",
							message: "请选择要推送的数据"
						})
						return
					}
    				break
    		}
    	},
    	//查询时间
    	changeDate (val) {
    		const startTime1 = val ? val[0] : ''
    		this.ruleForm.bigenTime = startTime1 ? new Date(startTime1).getTime() : undefined
    		const endTime1 = val ? val[1] : ''
    		this.ruleForm.endTime = endTime1 ? new Date(endTime1).getTime() : undefined
    	},
    	loadCity(){
    	    this.axios.post(api.CITYLIST).then(res => {
    	        if (res.data.code !== 200) {
    	            this.$message(res.data.msg)
    	        }else {
    	            this.citys = res.data.data.cityList
    	            this.citys.unshift({id:undefined,city:'全部'})
    	        }
    	    })
    	}
    }
}
</script>
<style lang="stylus" scoped>
    .pushPaymentRecord {
        
    }
</style>